// firstPackage/pages/flashSale/flashSale.js
import { queryNewHouseByCondition } from "../../../service/house_service"
import { attentionAction } from "../../../service/attention_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    houseList: [

    ],//房源列表
    scrollRefreshStatus: true,//下拉刷新状态
    isShowLoading: false,
    isBottomShow: false,
    requestData: {
      cityName: "成都市",
      houseStyle: "",
      maxArea: null,
      maxTotalPrice: null,
      maxUnitPrice: null,
      minArea: null,
      minTotalPrice: null,
      minUnitPrice: null,
      page: 0,
      pageSize: 10,
      saleStatus: "",
      subway: "",
      tags: ["限时优惠"],
      words: "",
      zone: ""
    },

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryNewHouseByCondition();
  },

  /**
    * 生命周期函数--监听页面显示
    */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },

  //条件查询房源:
  queryNewHouseByCondition() {
    wx.showLoading();
    let pageNum = this.data.requestData.page;
    let pageSize = this.data.requestData.pageSize;
    queryNewHouseByCondition(this.data.requestData).then(res => {
      this.setData({
        scrollRefreshStatus: false,
      })
      wx.hideLoading();
      console.log("JJLog:房源列表:" + JSON.stringify(res));
      if (res.code == 200) {
        let tempArr = [];
        tempArr = this.data.houseList;
        if (tempArr.length > 0 && pageNum > 0) {
          //加载下一页:
          tempArr = tempArr.concat(res.data);
          if (res.data.length < pageSize) {
            this.setData({
              isShowLoading: false,
              isBottomShow: true,
            })
            this.setData({
              houseList: tempArr
            })
          } else if (res.data.length == pageSize) {
            this.setData({
              isShowLoading: false,
            })
            this.setData({
              houseList: tempArr
            })
          }
        } else {
          this.setData({
            isShowLoading: false,
          })
          this.setData({
            houseList: res.data
          })
          if (res.data.length < pageSize) {
            this.setData({
              isBottomShow: true
            })
          }
        }
      }
    }).catch(err => {
      this.setData({
        scrollRefreshStatus: false,
      })
      console.error("JJLog:查询房源失败:" + JSON.stringify(err));
      wx.hideLoading();
      let page = this.data.requestData.page;
      if (page > 0) {
        page = page - 1;
      }
      this.setData({
        isShowLoading: false,
        'requestData.page': page
      })
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
    })
  },

  //点赞操作:
  collectAction(e) {
    let { data, index } = e.detail;
    this.attentionAction(data, index);
  },

  //关注操作
  attentionAction(item, index) {
    let data = {
      collectType: 1,
      number: item.houseNumber
    }
    wx.showLoading();
    attentionAction(data).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        item.isCollect = !item.isCollect;
        //动态数组:
        let changeItem = 'houseList[' + index + '].isCollect';
        this.setData({
          [changeItem]: item.isCollect,
        })
        wx.showToast({
          title: item.isCollect ? "关注成功" : "取关成功",
          icon: "none"
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:attentionAction", err);
    })
  },

  // scroll下拉刷新:
  refreshDownData() {
    console.log("JJLog:下拉刷新来了=====:");
    this.setData({
      scrollRefreshStatus: true,
      'requestData.page': 0,
      isBottomShow: false,
    })
    this.queryNewHouseByCondition();
  },
  //滑动列表滚动到底了啊:
  scrolltolower(e) {
    if (this.data.isBottomShow) {
      //已经到底了就不加载了
      return;
    }
    let tempPage = this.data.requestData.page + 1;
    this.setData({
      'requestData.page': tempPage,
      isShowLoading: true,
    })
    this.queryNewHouseByCondition();
  },








 
})